package viewPackage;

import java.awt.*;
import javax.swing.*;

import modelPackage.Point3D;
import modelPackage.WorldManager;
import modelPackage.WorldManagerInterface;

@SuppressWarnings("serial")
public class DrawingManager3D extends JPanel {

	private WorldManagerInterface WMan;
	private String PlayerID;
	static private int TILE_SIZE = 50;
	static private int MAP_DEPTH = 12;
	
	private PaneDimensions maxPane;
	private PaneDimensions vanishingPane;
	
	private PaneDimensions playerPane;
	private int playerPaneDepth;
	
	private Point origin;	
	private Line topLeft;
	private Line diagonal;
	
	
	public DrawingManager3D(WorldManager world, String ID, int x, int y) {
		super();
		WMan = world;
		PlayerID = ID;
		this.setBounds(0, 0, x, y);
				
		//Calculate the top left point of the perspective square
		int dX = (this.getWidth() - MAP_DEPTH * TILE_SIZE) / 2;
		int dY = (this.getHeight() - (WMan.getPlayer(PlayerID).getMap().getHeight() * TILE_SIZE)) / 2;
		origin = new Point(dX, dY);
		
		// Here we calculate the origin, height and width of the biggest and smallest panes.
			
		//The biggest one is pretty straight forward.
		maxPane = new PaneDimensions(origin, TILE_SIZE*MAP_DEPTH, WMan.getPlayer(PlayerID).getMap().getHeight() * TILE_SIZE);
				
		//Calculate the corners of the vanishing pane, which is the last pane we draw.
		int vPlaneW = (TILE_SIZE * MAP_DEPTH) / 4;
		int vPlaneH = (WMan.getPlayer(PlayerID).getMap().getHeight() * TILE_SIZE) / 4;
		int vX = (this.getWidth() - vPlaneW) / 2;
		int vY = (this.getHeight() - vPlaneH) / 2;
		vanishingPane = new PaneDimensions(vX, vY, vPlaneW, vPlaneH);
		
		//Define two models of lines.  These are used extensively in the world to screen function.
		topLeft = new Line(origin, new Point(vanishingPane.getOrigin().x, vanishingPane.getOrigin().y));
		diagonal = new Line(origin, new Point(vanishingPane.getOrigin().x + vanishingPane.getDimensions().x, vanishingPane.getOrigin().y));
		
		//Now set a pane for the player to be in all the time.
		playerPaneDepth = 3*TILE_SIZE;
		playerPane = this.getPane(playerPaneDepth);
		
	}
	
	@Override
	public void repaint() {
		super.repaint();
	}
	
	@Override
	public void paintComponent(Graphics g) {
			
		//Now I need to go through and draw stuff, same as 2D.
		int h = WMan.getPlayer(PlayerID).getMap().getHeight();
		int w = WMan.getPlayer(PlayerID).getMap().getWidth();
		
		int playerX = WMan.getPlayer(PlayerID).getPosition().x;
		int closestX = playerX - playerPaneDepth - 2*TILE_SIZE;
		int furthestX = playerX + ((13*TILE_SIZE) - playerPaneDepth);
		
		int closestTile = 0;
		if (closestX > 0) {
			closestTile = closestX / TILE_SIZE;
		}
		else {
			closestTile = 0;
		}
		
		int furthestTile = 0;
		if (furthestX < WMan.getPlayer(PlayerID).getMap().getWidth() * TILE_SIZE) {
			furthestTile = furthestX / TILE_SIZE;
		}
		else {
			furthestTile = WMan.getPlayer(PlayerID).getMap().getWidth() - 1;
		}
		
		//Draw a background, so old stuff gets erased.
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(Color.BLACK);
		
		//So we draw all the tiles that should be drawn.		
		for (int j = furthestTile; j >= closestTile; j--) {
			//First draw the edges of everything.
			for (int i = 0; i < h; i++) {
				for (int z = 0; z < MAP_DEPTH; z++) {	
				
					//Draw all the tiles at that particular Z.
					if (WMan.getPlayer(PlayerID).get3DMap().getMaps().get(z).getTiles().get(i*w + j).getSolid() == true) {
						
						//This is where to draw the tile.
						int realJ = playerPaneDepth - (playerX - WMan.getPlayer(PlayerID).getMap().getTiles().get(i*w + j).getPosition().x);
						
						//First scale the tile to the appropriate perspective size.
						Point screenLocation = worldToScreen(realJ, i*TILE_SIZE, z*TILE_SIZE);
						int w1 = this.widthToScreen(realJ, TILE_SIZE);
						int h1 = this.heightToScreen(realJ, TILE_SIZE);	
						
						//Now, find the coordinates of the same square scaled back by one more tile_size
						Point backLocation = worldToScreen((realJ)+TILE_SIZE, i*TILE_SIZE, z*TILE_SIZE);
						int w2 = this.widthToScreen((realJ)+TILE_SIZE, TILE_SIZE);
						int h2 = this.heightToScreen((realJ)+TILE_SIZE, TILE_SIZE);
						
						//Now, define some rectangles and stuff!
						PerspectiveRectangle left = new PerspectiveRectangle(screenLocation, backLocation, new Point(backLocation.x, backLocation.y+h2), new Point(screenLocation.x, screenLocation.y+h1));
						PerspectiveRectangle top = new PerspectiveRectangle(backLocation, new Point(backLocation.x+w2, backLocation.y), new Point(screenLocation.x+w1, screenLocation.y), screenLocation);
						PerspectiveRectangle right = new PerspectiveRectangle(new Point(screenLocation.x+w1, screenLocation.y), new Point(backLocation.x+w2, backLocation.y), new Point(backLocation.x+w2, backLocation.y+h2), new Point(screenLocation.x+w1, screenLocation.y+h1));
						PerspectiveRectangle bottom = new PerspectiveRectangle(new Point(backLocation.x, backLocation.y+h2), new Point(backLocation.x+w2, backLocation.y+h2), new Point(screenLocation.x+w1, screenLocation.y+h1), new Point(screenLocation.x, screenLocation.y+h1));
						
						Point tile = new Point((j*TILE_SIZE)+(TILE_SIZE/2), (i*TILE_SIZE)+(TILE_SIZE/2));
						
						//Now, if it would be normal to draw them, draw them.
						if (i*TILE_SIZE < (WMan.getPlayer(PlayerID).getMap().getHeight()*TILE_SIZE) / 2) {
							if (WMan.getPlayer(PlayerID).get3DMap().solidTileBelow(new Point3D(tile, z*TILE_SIZE)) == false) {
								bottom.paintComponent(g);
								g.setColor(Color.BLUE);
								bottom.fillComponent(g);
								g.setColor(Color.BLACK);
							}
						}
						else if (i*TILE_SIZE > (WMan.getPlayer(PlayerID).getMap().getHeight()*TILE_SIZE) / 2) {
							if (WMan.getPlayer(PlayerID).get3DMap().solidTileAbove(new Point3D(tile, z*TILE_SIZE)) == false) {
								top.paintComponent(g);
								if (WMan.getPlayer(PlayerID).get3DMap().getMaps().get(z).solidTileAnywhereAbove(WMan.getPlayer(PlayerID).get3DMap().getMaps().get(z).getTiles().get(i*w + j)) == false) {
									g.setColor(Color.BLUE);
								}								
								top.fillComponent(g);
								g.setColor(Color.BLACK);
							}
						}
						else {
							//Do nothing.
						}
						
						if (z*TILE_SIZE < (MAP_DEPTH*TILE_SIZE) /2) {
							if (WMan.getPlayer(PlayerID).get3DMap().solidTileRight(new Point3D(tile,z*TILE_SIZE)) == false) {
								right.paintComponent(g);
								g.setColor(Color.BLUE);
								right.fillComponent(g);
								g.setColor(Color.BLACK);
							}
						}
						else if (z*TILE_SIZE > (MAP_DEPTH*TILE_SIZE) /2) {
							if (WMan.getPlayer(PlayerID).get3DMap().solidTileLeft(new Point3D(tile,z*TILE_SIZE)) == false) {
								left.paintComponent(g);
								g.setColor(Color.BLUE);
								left.fillComponent(g);
								g.setColor(Color.BLACK);
							}
						}
						else {
							//Do nothing.
						}
		
					}	
					
					
					//Then draw all the players.
					if (WMan.getPlayers().size() != 0) {
						for (int k = 0; k < WMan.getPlayers().size(); k++) {
							if (WMan.getPlayers().get(k).getPosition().x >= (j*TILE_SIZE) && WMan.getPlayers().get(k).getPosition().x < (j*TILE_SIZE)+TILE_SIZE) {
								Point screen = this.worldToScreen(playerPaneDepth, WMan.getPlayers().get(k).getPosition().y, (int) WMan.getPlayers().get(k).get3DPosition().z);
								int wp = this.widthToScreen(playerPaneDepth, WMan.getPlayers().get(k).getWidth());
								int hp = this.heightToScreen(playerPaneDepth, WMan.getPlayers().get(k).getHeight());
								
								Point back = this.worldToScreen(playerPaneDepth+WMan.getPlayers().get(k).getWidth(), WMan.getPlayers().get(k).getPosition().y, (int) WMan.getPlayers().get(k).get3DPosition().z);
								int wpb = this.widthToScreen(playerPaneDepth+WMan.getPlayers().get(k).getWidth(), WMan.getPlayers().get(k).getWidth());
								int hpb = this.heightToScreen(playerPaneDepth+WMan.getPlayers().get(k).getWidth(), WMan.getPlayers().get(k).getHeight());
								
								//Now, define some rectangles and stuff!
								PerspectiveRectangle left = new PerspectiveRectangle(screen, back, new Point(back.x, back.y+hpb), new Point(screen.x, screen.y+hp));
								PerspectiveRectangle top = new PerspectiveRectangle(back, new Point(back.x+wpb, back.y), new Point(screen.x+wp, screen.y), screen);
								PerspectiveRectangle right = new PerspectiveRectangle(new Point(screen.x+wp, screen.y), new Point(back.x+wpb, back.y), new Point(back.x+wpb, back.y+hpb), new Point(screen.x+wp, screen.y+hp));
								PerspectiveRectangle bottom = new PerspectiveRectangle(new Point(back.x, back.y+hpb), new Point(back.x+wpb, back.y+hpb), new Point(screen.x+wp, screen.y+hp), new Point(screen.x, screen.y+hp));
								
								Point tile = new Point(WMan.getPlayers().get(k).getPosition().x+(WMan.getPlayers().get(k).getWidth()/2), WMan.getPlayers().get(k).getPosition().y+(WMan.getPlayers().get(k).getHeight()/2));
								
								//Now, if it would be normal to draw them, draw them.
								if (WMan.getPlayers().get(k).getPosition().y < (WMan.getPlayers().get(k).getMap().getHeight()*TILE_SIZE) / 2) {
									if (WMan.getPlayer(PlayerID).get3DMap().solidTileBelow(new Point3D(tile, z*TILE_SIZE)) == false) {
										bottom.paintComponent(g);
										g.setColor(Color.RED);
										bottom.fillComponent(g);
										g.setColor(Color.BLACK);
									}
								}
								else if (WMan.getPlayers().get(k).getPosition().y > (WMan.getPlayers().get(k).getMap().getHeight()*TILE_SIZE) / 2) {
									if (WMan.getPlayer(PlayerID).get3DMap().solidTileAbove(new Point3D(tile, z*TILE_SIZE)) == false) {
										top.paintComponent(g);
										g.setColor(Color.RED);
										top.fillComponent(g);
										g.setColor(Color.BLACK);
									}
								}
								else {
									//Do nothing.
								}
								
								
								if (WMan.getPlayers().get(k).get3DPosition().z < (MAP_DEPTH*TILE_SIZE) /2) {
									if (WMan.getPlayer(PlayerID).get3DMap().solidTileRight(new Point3D(tile, z*TILE_SIZE)) == false) {
										right.paintComponent(g);
										g.setColor(Color.RED);
										right.fillComponent(g);
										g.setColor(Color.BLACK);
									}
								}
								else if (WMan.getPlayers().get(k).get3DPosition().z > (MAP_DEPTH*TILE_SIZE) /2) {
									if (WMan.getPlayer(PlayerID).get3DMap().solidTileLeft(new Point3D(tile, z*TILE_SIZE)) == false) {
										left.paintComponent(g);
										g.setColor(Color.RED);
										left.fillComponent(g);
										g.setColor(Color.BLACK);
									}
								}
								else {
									//Do nothing.
								}									
							}
						}
					}
					
					//Then draw all the sprites.
					if (WMan.getPlayer(PlayerID).getMap().getSprites().size() != 0) {
						for (int n = 0; n < WMan.getPlayer(PlayerID).getMap().getSprites().size(); n++) {
							if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getDead() == false) {
								if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x >= (j*TILE_SIZE) && WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x < (j*TILE_SIZE)+TILE_SIZE) {
									Point screen = this.worldToScreen(playerPaneDepth-(WMan.getPlayer(PlayerID).getPosition().x-WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().y, (int) WMan.getPlayer(PlayerID).getMap().getSprites().get(n).get3DPosition().z);
									int wp = this.widthToScreen(playerPaneDepth - (WMan.getPlayer(PlayerID).getPosition().x - WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getWidth());
									int hp = this.heightToScreen(playerPaneDepth-(WMan.getPlayer(PlayerID).getPosition().x-WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getHeight());
									
									Point back = this.worldToScreen(playerPaneDepth-(WMan.getPlayer(PlayerID).getPosition().x-WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x) + WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getWidth(), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().y, (int) WMan.getPlayer(PlayerID).getMap().getSprites().get(n).get3DPosition().z);
									int wpb = this.widthToScreen(playerPaneDepth-(WMan.getPlayer(PlayerID).getPosition().x-WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x)+WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getWidth(), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getWidth());
									int hpb = this.heightToScreen(playerPaneDepth-(WMan.getPlayer(PlayerID).getPosition().x-WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x)+WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getWidth(), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getHeight());
									
									//Now, define some rectangles and stuff!
									PerspectiveRectangle left = new PerspectiveRectangle(screen, back, new Point(back.x, back.y+hpb), new Point(screen.x, screen.y+hp));
									PerspectiveRectangle top = new PerspectiveRectangle(back, new Point(back.x+wpb, back.y), new Point(screen.x+wp, screen.y), screen);
									PerspectiveRectangle right = new PerspectiveRectangle(new Point(screen.x+wp, screen.y), new Point(back.x+wpb, back.y), new Point(back.x+wpb, back.y+hpb), new Point(screen.x+wp, screen.y+hp));
									PerspectiveRectangle bottom = new PerspectiveRectangle(new Point(back.x, back.y+hpb), new Point(back.x+wpb, back.y+hpb), new Point(screen.x+wp, screen.y+hp), new Point(screen.x, screen.y+hp));
									
									Point tile = new Point(WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x+(WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getWidth()/2), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().y+(WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getHeight()/2));
									
									//Now, if it would be normal to draw them, draw them.
									if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().y < (WMan.getPlayer(PlayerID).getMap().getHeight()*TILE_SIZE) / 2) {
										if (WMan.getPlayer(PlayerID).get3DMap().solidTileBelow(new Point3D(tile, z*TILE_SIZE)) == false) {
											bottom.paintComponent(g);
											g.setColor(Color.GREEN);
											bottom.fillComponent(g);
											g.setColor(Color.BLACK);
										}
									}
									else if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().y > (WMan.getPlayer(PlayerID).getMap().getHeight()*TILE_SIZE) / 2) {
										if (WMan.getPlayer(PlayerID).get3DMap().solidTileAbove(new Point3D(tile, z*TILE_SIZE)) == false) {
											top.paintComponent(g);
											g.setColor(Color.GREEN);
											top.fillComponent(g);
											g.setColor(Color.BLACK);
										}
									}
									else {
										//Do nothing.
									}
									
									
									if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).get3DPosition().z < (MAP_DEPTH*TILE_SIZE) /2) {
										if (WMan.getPlayer(PlayerID).get3DMap().solidTileRight(new Point3D(tile, z*TILE_SIZE)) == false) {
											right.paintComponent(g);
											g.setColor(Color.GREEN);
											right.fillComponent(g);
											g.setColor(Color.BLACK);
										}
									}
									else if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).get3DPosition().z > (MAP_DEPTH*TILE_SIZE) /2) {
										if (WMan.getPlayer(PlayerID).get3DMap().solidTileLeft(new Point3D(tile, z*TILE_SIZE)) == false) {
											left.paintComponent(g);
											g.setColor(Color.GREEN);
											left.fillComponent(g);
											g.setColor(Color.BLACK);
										}
									}
									else {
										//Do nothing.
									}
								}
							}
						}
					}					
				}
			} 
			// then draw all the faces.
			for (int i = 0; i < h; i++) {
				for (int z = 0; z < MAP_DEPTH; z++) {	
				
					//Draw all the tiles at that particular Z.
					if (WMan.getPlayer(PlayerID).get3DMap().getMaps().get(z).getTiles().get(i*w + j).getSolid() == true) {
						
						//This is where to draw the tile.
						int realJ = playerPaneDepth - (playerX - WMan.getPlayer(PlayerID).getMap().getTiles().get(i*w + j).getPosition().x);
						
						//First scale the tile to the appropriate perspective size.
						Point screenLocation = worldToScreen(realJ, i*TILE_SIZE, z*TILE_SIZE);
						int w1 = this.widthToScreen(realJ, TILE_SIZE);
						int h1 = this.heightToScreen(realJ, TILE_SIZE);	
											
						g.drawRect(screenLocation.x, screenLocation.y, w1, h1);
						g.setColor(Color.BLUE);
						g.fillRect(screenLocation.x+1, screenLocation.y+1, w1-1, h1-1);
						g.setColor(Color.BLACK);
					}	
					
					
					//Then draw all the players.
					if (WMan.getPlayers().size() != 0) {
						for (int k = 0; k < WMan.getPlayers().size(); k++) {
							if (WMan.getPlayers().get(k).getPosition().x >= (j*TILE_SIZE) && WMan.getPlayers().get(k).getPosition().x < (j*TILE_SIZE)+TILE_SIZE) {
								Point screen = this.worldToScreen(playerPaneDepth, WMan.getPlayers().get(k).getPosition().y, (int) WMan.getPlayers().get(k).get3DPosition().z);
								int wp = this.widthToScreen(playerPaneDepth, WMan.getPlayers().get(k).getWidth());
								int hp = this.heightToScreen(playerPaneDepth, WMan.getPlayers().get(k).getHeight());
								
								g.drawRect(screen.x, screen.y, wp, hp);
								g.setColor(Color.RED);
								g.fillRect(screen.x+1, screen.y+1, wp-1, hp-1);
								g.setColor(Color.BLACK);
							}
						}
					}
					
					//Then draw all the sprites.
					if (WMan.getPlayer(PlayerID).getMap().getSprites().size() != 0) {
						for (int n = 0; n < WMan.getPlayer(PlayerID).getMap().getSprites().size(); n++) {
							if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getDead() == false) {
								if (WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x >= (j*TILE_SIZE) && WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x < (j*TILE_SIZE)+TILE_SIZE) {
									Point screen = this.worldToScreen(playerPaneDepth-(WMan.getPlayer(PlayerID).getPosition().x-WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().y, 0);
									//(int) WMan.getPlayer(PlayerID).getMap().getSprites().get(n).get3DPosition().z
									int wp = this.widthToScreen(playerPaneDepth - (WMan.getPlayer(PlayerID).getPosition().x - WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getWidth());
									int hp = this.heightToScreen(playerPaneDepth-(WMan.getPlayer(PlayerID).getPosition().x-WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getPosition().x), WMan.getPlayer(PlayerID).getMap().getSprites().get(n).getHeight());
									
									g.drawRect(screen.x, screen.y, wp, hp);
									g.setColor(Color.GREEN);
									g.fillRect(screen.x+1, screen.y+1, wp-1, hp-1);
									g.setColor(Color.BLACK);
								}
							}							
						}
					}					
				}
			}
		}	
	}
	
	
	private PaneDimensions getPane(int x) {
		//This is a calculation for perspective.
		//It is somewhat complex, and is based on line geometry.
		//It would be easier to describe with a picture, so just rest assured that it works.
		double xA = x * ((double)vanishingPane.getDimensions().x / (double)maxPane.getDimensions().x);
		
		if ((maxPane.getOrigin().x + x) == (vanishingPane.getOrigin().x + xA)) {
			xA+=1;
		}
		Line intersector = new Line(new Point(origin.x + x, origin.y), new Point(vanishingPane.getOrigin().x + (int)xA, vanishingPane.getOrigin().y));
		
		Point intersection = intersector.lineIntercept(diagonal);
		
		Line horizontal = new Line(0, intersection.y);
		
		Point paneOrigin = horizontal.lineIntercept(topLeft);
			
		//Now we need to find the width and height.  
		//Since each pane will be centered, this should be fairly straightforward.
		int difX = paneOrigin.x - origin.x;
		int difY = paneOrigin.y - origin.y;
			
		int width = maxPane.getDimensions().x - (2*difX);
		int height = maxPane.getDimensions().y - (2*difY);
		
		return new PaneDimensions(paneOrigin, width, height);
		
	}
	
	private Point worldToScreen(int x, int y, int z) {
		
		PaneDimensions pd = this.getPane(x);
		
		double paneW = (double) pd.getDimensions().x;
		double paneH = (double) pd.getDimensions().y;
		double maxW = (double) maxPane.getDimensions().x;
		double maxH = (double) maxPane.getDimensions().y;
		double zD = (double) z;
		double yD = (double) y;
		
		double xFinal = (zD * (paneW / maxW)) + pd.getOrigin().x;
		double yFinal = (yD * (paneH / maxH)) + pd.getOrigin().y;
		
		return new Point ((int) xFinal, (int) yFinal);
	}
	
	private int widthToScreen(int x, int w) {
		
		PaneDimensions pd = this.getPane(x);
		double wIN = (double) w;
		double paneWidth = (double) pd.getDimensions().x;
		double maxWidth = (double) maxPane.getDimensions().x;
		
		double w2 = (wIN*(paneWidth / maxWidth)); 
		
		return (int) w2;
	}
	
	private int heightToScreen(int x, int h) {

		PaneDimensions pd = this.getPane(x);
		double hIN = (double) h;
		double paneHeight = (double) pd.getDimensions().x;
		double maxHeight = (double) maxPane.getDimensions().x;
		
		double h2 = (hIN*(paneHeight / maxHeight)); 
		
		return (int) h2;
	}
	
	
}